package com.gopay.common.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.gopay.common.vo.ProgressBarVO;

/**
 * 进度条工具类
 * 
 * @ProgressBarUtil.java
 * @author dak.wang
 * @2016年3月22日 下午5:45:52 www.gopay.com.cn Inc.All rights reserved.
 */
public class ProgressBarUtil {
    private static final Logger log = Logger.getLogger(ProgressBarUtil.class);

    /**
     * 初始化进度条
     * 
     * @param request
     * @param bizId
     * @param total
     */
    public static synchronized void init(HttpServletRequest request, ProgressBarVO pbarVo) {
        request.getSession().setAttribute(pbarVo.getBizId(), pbarVo);
    }

    /**
     * 更新执行进度
     * 
     * @param request
     * @param bizId
     * @throws Exception
     */
    public static synchronized void update(HttpServletRequest request, ProgressBarVO vo) throws Exception {

        try {
            ProgressBarVO pbar = (ProgressBarVO) request.getSession().getAttribute(vo.getBizId());

            if (pbar.getCur() + vo.getStep() != pbar.getTotal()) {
                pbar.setCur(pbar.getCur() + vo.getStep());
                pbar.setDesc(vo.getDesc());
                request.getSession().setAttribute(vo.getBizId(), pbar);
            }

        } catch (Exception e) {
            log.info("--更新进度条失败--sessionKey" + vo.getBizId(), e);
            throw e;
        }
    }

    /**
     * 完成执行进度
     * 
     * @param request
     * @param bizId
     */
    public static synchronized void finish(HttpServletRequest request, ProgressBarVO vo) {
        try {
            ProgressBarVO pbar = (ProgressBarVO) request.getSession().getAttribute(vo.getBizId());
            pbar.setCur(pbar.getTotal());
            pbar.setDesc(vo.getDesc());
            pbar.setRespData(vo.getRespData());
            request.getSession().setAttribute(vo.getBizId(), pbar);
        } catch (Exception e) {
            log.info("--完成进度条失败--sessionKey" + vo.getBizId(), e);
            throw e;
        }
    }
}
